<?php

include_once "wxBizMsgCrypt.php";
$servername = "localhost";
$username = "jianzhan";
$password = "2DWFd2jhmSNjnSYL";
$dbname = "jianzhan";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 

$encodingAesKey = '6UIXxQOcCh6HQ5pdsXyBLKcpv0uf2SJGmjsX0bp0pRi';	//第三方平台解密key
$appId = 'wxc34321ba9f76f0ba';			//第三方平台appid
$appSecret = '';		//第三方平台appSecret
$token = 'rhwTvEn3BsPMv6ZSAGsJ3vS8JGahVH';			//第三方平台消息验证token
$timeStamp = empty ( $_GET ['timestamp']) ? "" : trim ( $_GET ['timestamp'] );
$nonce = empty ( $_GET ['nonce'] ) ?"" : trim ( $_GET ['nonce'] );
$msg_sign = empty ( $_GET['msg_signature'] ) ? "" : trim ( $_GET ['msg_signature'] );
$encryptMsg = file_get_contents ('php://input' );
$pc = new WXBizMsgCrypt ( $token,$encodingAesKey, $appId );
// 第三方收到公众号平台发送的消息
$msg = '';
$errCode = $pc->decryptMsg ($msg_sign, $timeStamp, $nonce, $encryptMsg, $msg );
if ($errCode == 0) {
    $data = _xmlToArr ( $msg);
    if (isset ( $data['ComponentVerifyTicket'] )) {
       //获取成功
    } elseif ($data ['InfoType'] =='unauthorized') {
        //取消授权
    }
    
    
    echo 'success';
    

    
    
    
} else {
    echo '解密失败'.$errCode;
}
 $ticket=$data['ComponentVerifyTicket'];   
$sql = "UPDATE `wewatch` SET `ticket`=$ticket id= 1";
 
if ($conn->query($sql) === TRUE) {
   
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
 
$conn->close(); 
 function _xmlToArr($xml) {
    $res = @simplexml_load_string ( $xml,NULL, LIBXML_NOCDATA );
    $res = json_decode ( json_encode ( $res), true );
    return $res;
}
